/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gemom.gui;

import com.gemom.util.CovMatrixData;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import java.util.*;
/**
 *
 * @author Yair Diaz
 */
public class VarianceSetItemsGUI extends JPanel {

    JCheckBox[] jcheckbox;
    JButton calculateVarbutton,resetCheckBoxesbutton,clearTextAreaButton;
    JPanel panelCheckboxes,panelTextAreaClearButton,panelCheckBoxTextAreaButtons;
    JTextArea textArea;
    private JScrollPane jscroll;
    final static String checkboxlabel= "Item";
    private Vector<String> vecItemsSelected;
    private CovMatrixData covM;
    
    public VarianceSetItemsGUI(int numberitems)
    {
        vecItemsSelected = new Vector<String>();
        
        jcheckbox = new JCheckBox[numberitems];
        for(int i=0;i<numberitems;i++)
            {jcheckbox[i]=new JCheckBox(checkboxlabel+i);}
        
        GridLayout gl=new GridLayout(0,8);
        gl.setVgap(0);
        panelCheckboxes = new JPanel(gl);
        panelCheckboxes.setBorder(BorderFactory.createTitledBorder
                        ("Select Items to calculate the variance"));
        
        for(int i=0;i<numberitems;i++)
            panelCheckboxes.add(jcheckbox[i]);
        
        textArea = new JTextArea("This text area will display the variance" +
                                 " of set of items...",8,50);
        textArea.setEditable(false);
        
        clearTextAreaButton= new JButton("Clear text Area");
        calculateVarbutton= new JButton("Calculate variance");
        resetCheckBoxesbutton= new JButton("Reset checkboxes");
        resetCheckBoxesbutton.setEnabled(false);
        clearTextAreaButton.setEnabled(false);
        calculateVarbutton.setEnabled(false);
        this.disableChecBoxes();
        
        panelTextAreaClearButton = new JPanel(new BorderLayout());
        panelTextAreaClearButton.add(BorderLayout.NORTH,textArea);
        panelTextAreaClearButton.add(BorderLayout.CENTER,clearTextAreaButton);
        textArea.setBorder(BorderFactory.createTitledBorder(""));
        
        
        
        
        panelCheckBoxTextAreaButtons = new JPanel(new GridBagLayout()); 
        GridBagConstraints c = new GridBagConstraints();
        Insets inset=new Insets(5,5,5,5);
	c.insets = inset;
        c.gridwidth = 3;
        c.gridx=0;c.gridy=0;
        panelCheckBoxTextAreaButtons.add(panelCheckboxes,c);
        c.gridwidth = 1;
        c.gridx=0;c.gridy=1;
        panelCheckBoxTextAreaButtons.add(calculateVarbutton,c);
        c.gridx=1;c.gridy=1;
        panelCheckBoxTextAreaButtons.add(resetCheckBoxesbutton,c);
        c.gridwidth = 3;
        c.gridx=0;c.gridy=2;
        panelCheckBoxTextAreaButtons.add(panelTextAreaClearButton,c);
        
      // this.add();
      
        jscroll= new JScrollPane(panelCheckBoxTextAreaButtons);
        jscroll.setPreferredSize(new Dimension(600,330));
  
        this.add(jscroll);
        
        /////////////////////////////////////////////////////////////
        
        resetCheckBoxesbutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        
             for(int i=0;i<jcheckbox.length;i++)
                    jcheckbox[i].setSelected(false);
        
        }});
        ////////////////////////////////////////////////////////////
       clearTextAreaButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {textArea.setText("");}});
         ////////////////////////////////////////////////////////////
            
       calculateVarbutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
    
            double var=covM.calculateVarsetItems(getItemsSelected(vecItemsSelected));
            
            String st= "VARIANCE("; 
            for(int i =0;i<getItemsSelected(vecItemsSelected).length;i++)
            {
                st+=getItemsSelected(vecItemsSelected)[i]+", ";
            }
            st+=") = "+var+"\n";       
                
            textArea.append(st);

  
            }});    
  
     ////////////////////////////////////////////////////////////
     ///////////////////CHECKBOXES///////////////////////////////
   for (int i = 0; i < jcheckbox.length; i++) 
      {jcheckbox[i].addItemListener(new ItemListener() {
       public void itemStateChanged(ItemEvent e) {
           JCheckBox jchckBx = (JCheckBox)e.getSource();    
           if(jchckBx.isSelected())
           {vecItemsSelected.add(jchckBx.getText().replaceAll(checkboxlabel,""));}
           else
           {vecItemsSelected.remove(jchckBx.getText().replaceAll(checkboxlabel,""));}
        }});
    }//end for-loop
//////////////////////////////////////////////////////////////////////////////// 
   
 }//end constructor
    
    public void clearTxtAreaAndCheckBoxes()
        {textArea.setText("");
        for(int i=0;i<jcheckbox.length;i++)jcheckbox[i].setSelected(false);}
    
    public void enableChecBoxes(){for(int i=0;i<jcheckbox.length;i++)jcheckbox[i].setEnabled(true);}
    public void disableChecBoxes(){for(int i=0;i<jcheckbox.length;i++)jcheckbox[i].setEnabled(false);}
    
    private int[] getItemsSelected(Vector<String> vecItemsSelect){   
            
            int[]itemselect = new int[vecItemsSelect.size()];
            for (int i = 0; i < vecItemsSelect.size(); i++)
                {itemselect[i]=Integer.parseInt(vecItemsSelect.get(i));}  
            return itemselect;
        }

    public void setCovMatrixDATA(CovMatrixData covM){this.covM=covM;}

}//end class



                 
            














































